package com.solocredit.finance.http

import com.solocredit.finance.BuildConfig
import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.data.analyse.AnalyseBuilder
import com.solocredit.finance.data.analyse.AnalyseUtil
import com.solocredit.finance.utils.helper.SharedHelper
import org.json.JSONObject
import java.util.Random


/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/24 14:12
 * @Annotation：接口API
 */
object ApiService {

    /**
     * http://159.138.165.72
     * https://www.vistafinance.net
     * 域名
     */
    const val MULLA_BASE_URL = "https://www.vistafinance.net"

    /**
     * 接口Url
     */
    object API_URL{
        /**
         * 地区转发
         * /v1/timezonePath
         */
        const val MULLA_API_TIMEZONE_URL = "/feLgaI8scyPZCvWfCu7o2w"
        /**
         * APP业务统一请求接口
         * /v1/commonApi
         */
        const val MULLA_API_COMMON_URL = "/podz4683nQjuV58Raoskw"
        /**
         * 图片上传
         * /v1/imageUpload
         */
        const val MULLA_API_UPLOAD_FILE_URL = "/nGftn3gT0lvvE06P9Q"
    }

    /**
     * 数据分析Path
     */
    object ANALYSIS_PATH{

        /**
         * googel install 请求路径
         * /v1/channelInstallTemp
         */
        const val MULLA_ANALYSIS_GOOGLE_INSTALL_PATH = "/iypbEVawFvo2GlX7gICuw"

        /**
         * adjust install 请求路径
         * /v1/adjustInstallTemp
         */
        const val MULLA_ANALYSIS_ADJUST_INSTALL_PATH = "/zz3ZUfhUM99Ym66qZP2A"

        /**
         * facebook install 请求路径
         * /v1/saveFbAppData
         */
        const val MULLA_ANALYSIS_FACEBOOK_INSTALL_PATH = "/s9uGrqHd8RujbNFEvmJw"

        /**
         * firebase 请求路径
         * /v1/fireBase/save
         */
        const val MULLA_ANALYSIS_FIREBASE_INFO_PATH = "/WlNlz5ti0caUmGO3KcHg"

        /**
         * AppList 请求路径
         * /v2/userAppList
         */
        const val MULLA_ANALYSIS_PHONE_APP_PATH = "/mdOVCLzay5PRT48tELXmmw"

        /**
         * 请求路径
         * /v2/smsRecord
         */
        const val MULLA_ANALYSIS_SMS_PATH = "/BL1Ov5iYLaLCbUZLASGNCw"

        /**
         * device 请求路径
         * /v1/deviceInfo
         */
        const val MULLA_ANALYSIS_DEVICE_PATH = "/kOEcYB0nv49sHOUXsMqiyQ"

        /**
         * battery 请求路径
         * /v1/userDeviceBattery
         */
        const val MULLA_ANALYSIS_BATTERY_PATH = "/s0539F0sCmkxrywd6VwtoJw"

        /**
         * hardwave 请求路径
         * /v1/userDeviceHardware
         */
        const val MULLA_ANALYSIS_HARDWAVE_PATH = "/trFQ65U7KsySLpbcD92ZOg"

        /**
         * contact 请求路径
         * /v2/phoneBookList
         */
        const val MULLA_ANALYSIS_CONTACT_PATH = "/Mmprz1eNec8CDZqjg07sAg"

        /**
         * storge 请求路径
         * /v1/userDeviceStorage
         */
        const val MULLA_ANALYSIS_STORGE_PATH = "/BCus1ud2Hg8XXq3ebY3r1Q"

        /**
         * wifi 请求路径
         * /v1/userDeviceWifi
         */
        const val MULLA_ANALYSIS_WIFI_PATH = "/hSalEPjbHsuAobCCCpwuPQ"

        /**
         * calllog 请求路径
         * /v2/callRecord
         */
        const val MULLA_ANALYSIS_CALL_RECORD_PATH = "/ZS93npPTqlZnBUBQFhzKA"
    }


    /**
     * 接口Path
     */
    object API_PATH{
        /**
         * APP配置参数 Path
         * /v1/getAppShowInfo
         */
        const val MULLA_API_APP_INFO_PATH = "/nzBQR7to3jZLNhJnOEAA"

        /**
         * 发送验证码
         * /v1/smsCode
         */
        const val MULLA_API_SEND_SMS_PATH = "/QmuFeNE1ENjBdbu0ceA"

        /**
         * 发送whatsapp验证码
         * /v1/sendWhatAppMessage
         */
        const val MULLA_API_WHATSAPP_SEND_CODE_PATH = "/NhDmtSySfdIkxXmqSRsXbQ"

        /**
         * 校验whatsapp验证码
         *  /v1/checkWhatAppMessageCode
         */
        const val MULLA_API_WHATSAPP_CHECK_CODE_PATH = "/INeB1h1RtVSJDsCRjui2mw"

        /**
         * 登录
         * /v1/userLogin
         */
        const val MULLA_API_LOGIN_PATH = "/jjKyHsVWX674TmrVRZfTQ"

        /**
         * 无验证码登录
         * /v1/userLoginNoCode
         */
        const val MULLA_API_LOGIN_NO_CODE_PATH = "/s32jEgUUBrZeYPCpammBzVg"

        /**
         * 用户流程状态
         * /v1/userProcess
         */
        const val MULLA_API_PROCESS_PATH = "/dnFZQb0wEZg3KmPwSXYIw"

        /**
         * 保存用户图片信息（身份证、护照、驾驶证、人脸）
         * /v1/ocrInfo
         */
        const val MULLA_API_OCR_INFO_PATH = "/yQD9lG5AOolOwW1eAtoQg"

        /**
         * 用户贷款额度
         * /v1/loanLimit
         */
        const val MULLA_API_LIMIT_PATH = "/AjBF0eH5YEXQnxdXd94FA"

        /**
         * 用户贷款额度信息（循环额度）
         * /v1/getLoanHomePage
         */
        const val MULLA_API_LOAN_LIMIT_PATH = "/nE3roKzMXXQ6e52NlyU6Q"

        /**
         * 新用户贷款额度预览信息
         * /v1/getLoanApplyDetail
         */
        const val MULLA_API_LOAN_APPLY_PATH = "/QJE9Eca14F0cS5crZRwfg"

        /**
         * 可选额度和期限的预览信息(老用户)
         * /getLoanDetailPageForDays
         */
        const val MULLA_API_LOAN_APPLY_CUSTOM_LIMIT_PATH = "/s9wHjSIVGP8X4Xx7MFU9A"

        /**
         * 创建贷款申请订单
         * /v1/loanApply
         */
        const val MULLA_API_LOAN_APPLY_ORDER_CREATE_PATH = "/s3gDDEguvfVNiOQTVfd2Qw"

        /**
         * 订单列表
         * /v1/getAllOrderList
         */
        const val MULLA_API_LOAN_ORDER_PATH = "/ECN78LBasC0E6yGcducQ"

        /**
         * 贷款还款信息
         * /v1/repayInfo
         */
        const val MULLA_API_LOAN_REPAYMENT_INFO_PATH = "/s0NZK0bD117LpGnH7WeMcg"

        /**
         * 贷款还款链接
         * /v1/orderRepayH5
         */
        const val MULLA_API_LOAN_REPAYMENT_URL_PATH = "/cwhPl0BR06XjwCYdsMmhw"

        /**
         * 贷款还款链接（肯尼亚、乌干达）
         * /v1/getOrderRepayH5
         */
        const val MULLA_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH = "/eHdiF4N1Zd1Pu5iKsdcmA"

        /**
         * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
         * /v1/queryPaymentStatus
         */
        const val MULLA_API_LOAN_REPAYMENT_STATUS_PATH = "/r1m2uv73zEcxSwvRtOsWrA"

        /**
         * 账户列表
         * /v1/getBindCardList
         */
        const val MULLA_API_ACCOUNT_LIST_PATH = "/ij4la6LIyS76K5wMotDPA"

        /**
         * 已绑定账户列表（法语）
         * /v2/fetchBoundBankAccount
         */
        const val MULLA_API_ACCOUNT_LIST_FRENCH_PATH = "/sFNoOgZbc054wvZarakeQ"

        /**
         * 添加账户(法语地区)
         * /v2/bindBankAccount
         */
        const val MULLA_API_ACCOUNT_ADD_PATH = "/Td2DimGq2B4ZVVjrWTEQRg"

        /**
         * 删除已绑定的账户
         * /v1/deleteBoundBankAccount
         */
        const val MULLA_API_ACCOUNT_DELETE_PATH = "/s2GaoAxB60JjjkE3ENbVA"

        /**
         * 评价弹框
         * /v1/fetchStarWindowConfig
         */
        const val MULLA_API_STAR_COMMENT_PATH = "/sH4mr5oLd6xeJ3h6UQ0oJw"

        /**
         * 提交意见反馈
         * /v1/feedback
         */
        const val MULLA_API_FEEDBACK_SUBMIT_PATH = "/layFp8se3mxW9gnKLtH4Aw"

        /**
         * 推荐APP列表
         * /v1/userDeniedDc
         */
        const val MULLA_API_RECOMMEND_APP_LIST_PATH = "/JdFIARGHj4HfVzrjlvufQ"

        /**
         * 事件埋点
         * /v1/userBuriedPoint
         */
        const val MULLA_API_TRACK_EVENT_PATH = "/ihnmivHe4IFc2OuY8CDw"

        /**
         * 事件批量埋点
         * v1/userBuriedPointForBatch
         */
        const val MULLA_API_TRACK_EVENT_BATCH_PATH = "/Hx234avwVjN9U3ewlQPziA"

        /**
         * 消息列表
         * /v1/fetchUserMessages
         */
        const val MULLA_API_MESSAGE_LIST_PATH = "/kd2H7cEsMbfs0kWmNFKumg"

        /**
         * 产品列表
         * /queryNewStyleProducts
         */
        const val MULLA_API_LOAN_PRODUCT_LIST_PATH = "/czHT0mb0U3EsPAW9xmrg"

        /**
         *  复制产品A信息 到 产品B (配置信息、进件信息)
         *  /oneKeyCopyToApply
         */
        const val MULLA_API_LOAN_PRODUCT_COPY_PATH = "/CTjEoNPLiEUAtwFLOFCn2Q"

        /**
         * 优惠券列表
         * /v1/getUserCouponAndScoreInfo
         */
        const val MULLA_API_COUPON_LIST_PATH = "/s1wrsaElGVx4vibDGpdI81w"

        /**
         * 签到信息
         * /v1/userSign
         */
        const val MULLA_API_SIGN_PATH = "/gHhsBpDSg9xfjV4UUMhzaA"

        /**
         *  存取积分、优惠券兑换
         *  /v1/changeUserScore
         */
        const val MULLA_API_SAVE_OR_USE_PATH = "/NZRSPm6VcVIFxE4IOc3Jyw"

        /**
         * 保存自定义参数接口
         * /v1/saveAbUserInfo
         */
        const val MULLA_API_SAVE_CUSTOM_PARAMS_PATH = "/KdCmqgFnZ6siNL8EqN4xtQ"

        /**
         * 获取自定义参数
         * /v1/getAbUserInfo
         */
        const val MULLA_API_LOAD_CUSTOM_PARAMS_PATH = "/L5L1zdCKvpRB9QL7EzEBPQ"

        /**
         * 清除用户数据
         * /v1/userCancel
         */
//        const val MULLA_API_USER_DATA_CLEAR_PATH = "/DCuIjztUYo3McAxBEH8Jwg"

        /**
         * 清除用户数据(新)
         * /v1/dropUserData
         */
        const val MULLA_API_USER_DATA_CLEAR_PATH = "/p1jNVxQBwGCXS3e0WdDcTw"

        /**
         * 设备数据上传状态
         * /v1/queryDeviceInfoTypes
         */
        const val MULLA_API_DEVICE_INFO_TYPES_PATH = "/CUkQLbJ70aK432MCucbitg"

    }

    /**
     * 获取请求Url
     */
    fun getHttpUrl(url: String): String{
        //地区转发
        val timeZone = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$MULLA_BASE_URL/$timeZone$url"
        }
        return "$MULLA_BASE_URL$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", MullaConfigs.MULLA_APPNAME)                                           //  产品 APP名称
            put("token", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_USERID))                                             //  userId
            put("version", BuildConfig.VERSION_CODE)                                        //  app版本号
            put("timp",System.currentTimeMillis())                                            //  时间戳
            put("country", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TIMEZONE_ID))                                          //当前时区id
            put("localTimestamp","${System.currentTimeMillis()}")                       //时间戳
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this
    }

    /**
     * 添加随机参数增量 8
     */
    fun JSONObject.addRandomParams(): JSONObject{
        val mobile = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_MOBILE)
        val timeStamp = System.currentTimeMillis() / 1000
        apply {
            put("loginName",mobile)
            put("loginTimeStamp",timeStamp)
            put("signName", AnalyseBuilder.product)
            put("signTimeStamp",timeStamp)
            put("luckyDrawName", AnalyseBuilder.product)
            put("luckyDrawTimeStamp",timeStamp)
            put("applyName", AnalyseBuilder.product)
            put("applyTimeStamp",timeStamp)
//            for (i in 0..7){
//                val addKey = getMullaRandomKey(1+Random().nextInt(4))
//                val addValue = getMullaRandomKey(1+Random().nextInt(16))
//                put(addKey.lowercase(),addValue.lowercase())
//            }
        }
        return this
    }

    /**
     * 随机字符串
     */
    private fun getMullaRandomKey(length: Int): String {
        val charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        val stringBuilder = StringBuilder()
        val random = Random()
        for (i in 0..length){
            val index = random.nextInt(charSet.length)
            stringBuilder.append(charSet[index]);
        }
        return stringBuilder.toString();
    }

    /**
     * 获取token
     */
    fun takeFlexiMolaToken(): String{
        return SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TOKEN)
    }
}